package utils;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class ConnectionUtils {
	static String username;
	static String pwd;
	static String url;
	static String driver;

	static {
		try {
			getParam();
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void getParam() throws IOException {
		FileInputStream fis = new FileInputStream("src/db_oracle.properties");
		Properties prop = new Properties();
		prop.load(fis);
		username = prop.getProperty("username");
		pwd = prop.getProperty("pwd");
		url = prop.getProperty("url");
		driver = prop.getProperty("driver");
	}

	public static Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url, username, pwd);
	}

	public static void close(Connection conn, Statement stm,PreparedStatement stmt, ResultSet rs) throws SQLException {
		if (rs != null) {
			rs.close();
		}
		if (stm != null) {
			stm.close();
		}
		if (stmt != null) {
			stmt.close();
		}
		if (conn != null) {
			conn.close();
		}
	}
}
